﻿using ConfClient;
using Google.Protobuf;
using Zpb;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using UtilLib;
using UtilPublic.Serialization;

namespace client
{
    public partial class MainWindow
    {
        public void MsgHandle(MsgData msg)
        {
            if (!(msg.code == EProtocolResult.成功 
                || (msg.code == EProtocolResult.玩家不存在 && msg.pid == EPid.GcplayerLogin)))
            {
                App.DebugError(msg.code.ToString()); return;
            }
            switch (msg.pid)
            {
                case EPid.Gcconnected:
                    {
                        App.Debug($"Connected");
                    }
                    break;
                case EPid.LcuserLogin:
                    {
                        L2CUserLogin rsp = new();
                        rsp.MergeFrom(new CodedInputStream(msg.buf));
                        GameData.UserId = rsp.UserId;
                        GameData.Token = rsp.Token;
                        {
                            C2GPlayerLogin req = new();
                            req.UserId = GameData.UserId;
                            req.Token = GameData.Token;
                            App.Send(EPid.CgplayerLogin, req);
                        }
                    }
                    break;
                case EPid.GcplayerLogin:
                    {
                        if (msg.code == EProtocolResult.玩家不存在)
                        {
                            C2GPlayerCreate req = new();
                            req.UserId = GameData.UserId;
                            req.Token = GameData.Token;
                            req.Name = "8i9o0p";
                            App.Send(EPid.CgplayerCreate, req);
                            return;
                        }
                        获取玩家信息();
                    }
                    break;
                case EPid.GcplayerReplace:
                    {
                        App.DebugError("你的账号在其他设备登录！"); 
                    }
                    break;
                case EPid.GcplayerCreate:
                    {
                        获取玩家信息();
                    }
                    break;
                case EPid.GcplayerInit:
                    {
                        PlayerInit rsp = new();
                        rsp.MergeFrom(new CodedInputStream(msg.buf));
                        GameData.PlayerInfoBase = rsp.InfoBase;
                        GameData.PlayerInfoMisc = rsp.InfoMisc;
                        GameData.ItemInfoList = rsp.InfoItems;
                        GameData.EquipInfoList = rsp.InfoEquips;
                        玩家登录成功();
                    }
                    break;
                case EPid.GcPlayerPushInfo:
                    {
                        PlayerInfo rsp = new();
                        rsp.MergeFrom(new CodedInputStream(msg.buf));
                        GameData.PlayerInfoBase = rsp.InfoBase;
                        GameData.PlayerInfoMisc = rsp.InfoMisc;
                        App.PagePlayer.UpdatePlayerBase();
                        App.PagePlayer.UpdatePlayerMisc();
                    }
                    break;
                case EPid.GcPlayerPushInfoBase:
                    {
                        PlayerInfoBase rsp = new();
                        rsp.MergeFrom(new CodedInputStream(msg.buf));
                        GameData.PlayerInfoBase = rsp;
                        App.PagePlayer.UpdatePlayerBase();
                    }
                    break;
                case EPid.GcPlayerPushInfoMisc:
                    {
                        PlayerInfoMisc rsp = new();
                        rsp.MergeFrom(new CodedInputStream(msg.buf));
                        GameData.PlayerInfoMisc = rsp;
                        App.PagePlayer.UpdatePlayerMisc();
                    }
                    break;
                case EPid.GcmailListGet:
                    {
                        MailInfoList rsp = new();
                        rsp.MergeFrom(new CodedInputStream(msg.buf));
                        GameData.MailInfoList = rsp;
                        App.PagePlayer.UpdateMailInfoList();
                    }
                    break;
                case EPid.GcitemInfoGet:
                case EPid.GcitemInfoPush:
                    {
                        ItemInfoList rsp = new();
                        rsp.MergeFrom(new CodedInputStream(msg.buf));
                        GameData.ItemInfoList = rsp;
                        App.PagePlayer.UpdateItemInfo();
                    }
                    break;
                case EPid.GcequipInfoGet:
                case EPid.GcequipInfoPush:
                    {
                        EquipInfoList rsp = new();
                        rsp.MergeFrom(new CodedInputStream(msg.buf));
                        GameData.EquipInfoList = rsp;
                        App.PagePlayer.UpdateEquipInfo();
                    }
                    break;

                default:
                    break;
            }
        }
        private void 获取玩家信息()
        {
            App.Send(EPid.CgplayerInit);
        }
        public void 玩家登录成功()
        {
            App.WdMain.ColumnGridNav.Width = new GridLength(100);
            App.WdMain.GridNav.Visibility = Visibility.Visible;
            //GameData.Player = rsp.Player;
            App.WdMain.PageOn("PagePlayer");
            //App.WdMain.PageOn("PageDebug");
            App.PagePlayer.UpdatePage();
        }
    }
}
